package com.mybatis_db_read_writer.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 数据库切面
 * @author nie_chaoqun
 * @date
 */
@Aspect
@Component
public class DataSourceAop {
    @Pointcut("!@annotation(com.nie.mybatis_db_read_writer.annotation.Master) " +
            "&& (execution(* com.nie.mybatis_db_read_writer.service..*.select*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.get*(..)))")
    public void readPointcut() {

    }

    @Pointcut("@annotation(com.nie.mybatis_db_read_writer.annotation.Master) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.insert*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.add*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_write.service..*.update*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.edit*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.delete*(..)) " +
            "|| execution(* com.nie.mybatis_db_read_writer.service..*.remove*(..))")
    public void writePointcut() {

    }

    @Before("readPointcut()")
    public void read() {
        DbContextHolder.slave();
    }

    @Before("writePointcut()")
    public void write() {
        DbContextHolder.master();
    }

}
